#!/usr/bin/env python
# -*- coding: cp1252 -*

import pygtk
import gtk
import pango
import UndoManager
import MMapArea
import gobject

class MindMapWrapper(gtk.Window):
    __gsignals__ = dict (title_changed      = (gobject.SIGNAL_RUN_FIRST,
                                               gobject.TYPE_NONE,
                                               (gobject.TYPE_STRING, gobject.TYPE_OBJECT)),
                         doc_save           = (gobject.SIGNAL_RUN_FIRST,
                                               gobject.TYPE_NONE,
                                               (gobject.TYPE_STRING, gobject.TYPE_OBJECT)),
                         file_saved         = (gobject.SIGNAL_RUN_FIRST,
                                               gobject.TYPE_NONE,
                                               (gobject.TYPE_STRING, gobject.TYPE_OBJECT)),
                         window_closed      = (gobject.SIGNAL_RUN_FIRST,
                                               gobject.TYPE_NONE,
                                               (gobject.TYPE_OBJECT, )))

    def __init__(self):
        super(MindMapWrapper, self).__init__()
        #define main window :
        self.mainWindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.mainWindow.set_default_size(610, 510)
        self.mainWindow.set_title('CharTr')
        self.mainWindow.connect('destroy', gtk.main_quit, 0)

        self.mainTable = gtk.Table(10,10,False)
        self.mainWindow.add(self.mainTable)

        button1 = gtk.Button()
        image1 = gtk.Image()
        image1.set_from_file("chartr_zoomin.png")
        button1.add(image1)
        self.mainTable.attach(button1, 0, 1, 0, 1,gtk.SHRINK,gtk.SHRINK)
        button1.connect('clicked', self.on_zoomIn)

        button2 = gtk.Button()
        image2 = gtk.Image()
        image2.set_from_file("chartr_zoomout.png")
        button2.add(image2)
        self.mainTable.attach(button2,1,2,0,1,gtk.SHRINK,gtk.SHRINK)
        button2.connect('clicked',self.on_zoomOut)

        button3 = gtk.Button()
        image3 = gtk.Image()
        image3.set_from_file("chartr_image.png")
        button3.add(image3)
        self.mainTable.attach(button3,2,3,0,1,gtk.SHRINK,gtk.SHRINK)
        button3.connect('clicked',self.on_img)
        
        button4 = gtk.Button()
        image4 = gtk.Image()
        image4.set_from_file("chartr_note.png")
        button4.add(image4)
        self.mainTable.attach(button4,3,4,0,1,gtk.SHRINK,gtk.SHRINK)
        button4.connect('clicked',self.on_note)

        button5 = gtk.Button()
        image5 = gtk.Image()
        image5.set_from_file("chartr_audio.png")
        button5.add(image5)
        self.mainTable.attach(button5,4,5,0,1,gtk.SHRINK,gtk.SHRINK)
        button5.connect('clicked',self.on_audio)
        
        #button6 = gtk.Button('Undo')
        #self.mainTable.attach(button6,5,6,0,1,gtk.SHRINK,gtk.SHRINK)
        #button6.connect('clicked',self.on_undo)


        # First, construct the MainArea and connect it all up
        self.undo = UndoManager.UndoManager (self)
        self.undo.block ()
        self.MainArea = MMapArea.MMapArea (self.undo)
        self.MainArea.connect ("title_changed", self.title_changed_cb)
        self.MainArea.connect ("doc_save", self.doc_save_cb)
        self.MainArea.connect ("doc_delete", self.doc_del_cb)
        self.MainArea.connect ("change_mode", self.mode_request_cb)
        self.MainArea.connect ("button-press-event", self.main_area_focus_cb)
        self.MainArea.connect ("change_buffer", self.switch_buffer_cb)
        self.MainArea.connect ("text_selection_changed", self.selection_changed_cb)
        self.MainArea.connect ("set_focus", self.main_area_focus_cb)
        # Add in the extended info view
        self.extended = gtk.TextView ()
        self.extended.set_wrap_mode (gtk.WRAP_WORD_CHAR)
        self.invisible_buffer = gtk.TextBuffer ()

        self.MainArea.set_size_request(600, 500)
        self.pane_pos = 500
        self.mode = MMapArea.MODE_EDITING
        self.extended_visible = False

        self.mainTable.attach(self.MainArea,0,10,1,10,gtk.EXPAND,gtk.EXPAND)
        self.mainWindow.show_all()
        self.connect ("configure_event", self.configure_cb)
        self.connect ("window-state-event", self.window_state_cb)


    def on_undo(self,widget):
        self.MainArea.undo_delete()
        self.MainArea.invalidate()

    def on_zoomOut(self,widget):
        self.MainArea.scale_fac = 0.95 * self.MainArea.scale_fac
        self.MainArea.invalidate()

    def on_zoomIn(self,widget):
        self.MainArea.scale_fac = 1.05 * self.MainArea.scale_fac
        self.MainArea.invalidate()

    def on_img(self,widget):
        coords = self.MainArea.get_selected_thought_coords()
        if coords[0]!=0 or coords[1]!=0:
            thought = self.MainArea.create_new_thought(coords,1)
            self.MainArea.create_link (self.MainArea.selected[0], None, thought)
            
            self.MainArea.invalidate()

    def on_note(self,widget):
        coords = self.MainArea.get_selected_thought_coords()
        if coords[0]!=0 or coords[1]!=0:
            thought = self.MainArea.create_new_thought(coords,3)
            self.MainArea.create_link (self.MainArea.selected[0], None, thought)
            
            self.MainArea.invalidate()

    def on_audio(self,widget):
        coords = self.MainArea.get_selected_thought_coords()
        if coords[0]!=0 or coords[1]!=0:
            thought = self.MainArea.create_new_thought(coords,4)
            self.MainArea.create_link (self.MainArea.selected[0], None, thought)
            
            self.MainArea.invalidate()

    def window_state_cb (self, window, event):
        if event.changed_mask & gtk.gdk.WINDOW_STATE_MAXIMIZED:
            self.maximised = not self.maximised

    def close_window_cb (self, event):
        self.SaveTimer.cancel = True
        self.hide ()
        self.MainArea.save_thyself ()
        del (self)

    def configure_cb (self, window, event):
        self.xpos = event.x
        self.ypos = event.y
        self.width = event.width
        self.height = event.height
        return False


    def doc_save_cb (self, widget, doc, top_element):
        save_string = self.serialize_to_xml(doc, top_element)
        if not self.save_file:
            sham = sha.new (save_string)
            save_loc = utils.get_save_dir ()
            self.save_file = save_loc+sham.hexdigest()+".map"
            counter = 1
            while os.path.exists(self.save_file):
            
                print "Warning: Duplicate File.  Saving to alternative"
                self.save_file = save_loc + "Dup"+str(counter)+sham.hexdigest()+".map"
                counter += 1

        self.save_map(self.save_file, save_string)
        self.emit ('file_saved', self.save_file, self)

    def switch_buffer_cb (self, arg, new_buffer):
        if new_buffer:
            self.extended.set_editable (True)
            self.extended.set_buffer (new_buffer)
        else:
            self.extended.set_buffer (self.invisible_buffer)
            self.extended.set_editable (False)
        
    def main_area_focus_cb (self, arg, event, extended = False):
        if not extended:
            self.MainArea.grab_focus ()
        else:
            self.extended.grab_focus ()

    def thought_selected_cb (self, arg, background_color, foreground_color):
        if background_color:
            self.background_widget.set_color(background_color)
        if foreground_color:
            self.foreground_widget.set_color(foreground_color)

    def selection_changed_cb (self, area, start, end, text):
        clip = gtk.Clipboard (selection="PRIMARY")
        if text:
            clip.set_text (text)
        else:
            clip.clear ()

    def switch_buffer_cb (self, arg, new_buffer):
        if new_buffer:
            self.extended.set_editable (True)
            self.extended.set_buffer (new_buffer)
        else:
            self.extended.set_buffer (self.invisible_buffer)
            self.extended.set_editable (False)
            
    def mode_request_cb (self, widget, mode):
        if self.set_val:
            self.act.set_current_value (mode)
        else:
            pass

    def thought_selected_cb (self, arg, background_color, foreground_color):
        if background_color:
            self.background_widget.set_color(background_color)
        if foreground_color:
            self.foreground_widget.set_color(foreground_color)
        

    def doc_del_cb (self, widget):
        self.emit ('window_closed', None)

    def selection_changed_cb (self, area, start, end, text):
        clip = gtk.Clipboard (selection="PRIMARY")
        if text:
            clip.set_text (text)
        else:
            clip.clear ()


    def title_changed_cb (self, widget, new_title):
        self.title_cp = ''
        if new_title == '':
            pass
        else:
            split = new_title.splitlines ()
            if split:
                final = split.pop ()
                for s in split:
                    self.title_cp += s
                    self.title_cp += ' '
                self.title_cp += final
        if len(self.title_cp) > 27:
            x = self.title_cp[0:27]+"..."
            self.mainWindow.set_title (x)
        else:
            self.mainWindow.set_title (self.title_cp)
        #self.emit ("title-changed", self.title_cp, self)

    def configure_cb (self, window, event):
        self.xpos = event.x
        self.ypos = event.y
        self.width = event.width
        self.height = event.height
        return False

    def window_state_cb (self, window, event):
        if event.changed_mask & gtk.gdk.WINDOW_STATE_MAXIMIZED:
            self.maximised = not self.maximised


 
 
class CharTr():
    def __init__(self):
        self.mindmap = MindMapWrapper()    
        #self.mindmap.show()

def main():
    gtk.main()
    return 0       
    
if __name__ == "__main__":
    CharTr()
    main()
  

